<template>
  <div class="active">
    <h3>供应商资格审查</h3>
    <el-table :data="filetable" style="width: 100%">
      <!-- 使用 v-for 循环生成表格列 -->

      <el-table-column prop="status" label="序号" width="80" align="center">
        <template #default="scope">
          <span class="ellipsis">{{ scope.row.key }}</span>
        </template>
      </el-table-column>

      <el-table-column
        prop="textValue"
        label="实质性响应条款"
        width="200"
        align="center"
      >
        <template #default="scope">
          <span class="ellipsis">{{ scope.row.textValue }}</span>
        </template>
      </el-table-column>
      <el-table-column
        v-for="(column, index) in fileColumns"
        :key="index"
        :prop="column.prop"
        :label="column.label"
        :width="column.width"
        align="center"
      >
        <!-- 自定义表头 -->
        <template #header="{ column: columnHeader }">
          <div class="link" @click="ClickRow(column)">
            {{ columnHeader.label }}
          </div>
        </template>
        <template #default="scope">
          <el-select
            :class="
              scope.row[column.prop] == 'apply_supplier_no_pass' ? 'No' : ''
            "
            v-model="scope.row[column.prop]"
            :placeholder="`请选择`"
            style="width: 100%; color: red"
            @change="handleSelectChange(scope.row, column, $event)"
          >
            <el-option
              v-for="(option, i) in options"
              :key="i"
              :label="option.label"
              :value="option.value"
            />
          </el-select>
        </template>
      </el-table-column>

      <!-- 手动添加操作列 -->
      <!-- <el-table-column label="备注" width="120" fixed="right" align="center">
        <template #default="scope">
          <el-input
            @blur="editRemark(scope.row, scope.row.remark)"
            v-model="scope.row.remark"
            placeholder="请输入备注"
          ></el-input>
        </template>
      </el-table-column> -->
    </el-table>
    <h3 v-if="!Stop">项目中止恢复请示材料</h3>
    <el-table :data="restoretable" style="width: 100%" v-if="!Stop">
      <el-table-column prop="key" label="序号" width="80"></el-table-column>
      <el-table-column
        prop="reviewTime"
        label="评审时间"
        width="180"
      ></el-table-column>
      <el-table-column
        prop="projectName"
        label="项目请示材料名称"
        width="600"
      ></el-table-column>
      <el-table-column
        label="操作"
        v-if="restoretable[0] && restoretable[0].pushValue"
      >
        <template #default="scope">
          <el-button type="text" @click="previewFile(scope.row)"
            >查看</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div
      style="
        display: flex;
        justify-content: center;
        align-items: center;
        margin-top: 30px;
      "
      v-if="Stop"
    >
      <el-button type="primary" @click="nextStep" :disabled="passNum == 2"
        >下一步</el-button
      >
      <el-button type="primary" plain @click="nextStep" :disabled="passNum != 2"
        >申请评审中止</el-button
      >
    </div>
    <div
      style="
        display: flex;
        justify-content: center;
        align-items: center;
        margin-top: 30px;
      "
      v-else
    >
      <el-button type="primary" @click="nextStep">下一步</el-button>
    </div>
    <div class="hidden-area" :style="{ left: HiddenArea ? '0' : '-32%' }">
      <div
        v-if="!HiddenArea && info.id"
        class="open"
        @click="HiddenArea = true"
      >
        <img src="@/assets/right.png" alt="" class="icoButtom" />
      </div>
      <div v-else class="hidden-div">
        <div class="hidden-info">
          <div v-if="info.id" class="custom-scrollbar">
            <div class="info-logo">
              <img src="@/assets/2de0a0aed8c056d27f6076574092ce6.png" alt="" />
            </div>
            <biddingInfo
              :id="info.id"
              :judgeid="info.judgeid"
              :back="false"
              :pattern="pattern"
            />
          </div>
        </div>
        <div @click="HiddenArea = false" class="close">
          <img src="@/assets/left.png" alt="" class="icoButtom" />
        </div>
      </div>
    </div>
  </div>
</template>
<script lang="ts" setup>
import { ref } from "vue";
import { useRoute, useRouter } from "vue-router";
import { ElMessage } from "element-plus";
import {
  getDictList,
  getDict,
  nowTime,
  getdict,
  SearchDicte,
} from "@/utils/common.js";
import {
  execTask,
  applySupplierInfoByProjectId,
  updateSupplierPushValue,
  updateApplySupplier,
} from "@/api/examine.js";

const emit = defineEmits(["next-step"]);
const HiddenArea = ref(false);
const info = ref({});
const route = useRoute();
const id = computed(() => route.query.id);
const options = ref([
  { value: "apply_supplier_pass", label: "通过状态" },
  { value: "apply_supplier_no_pass", label: "未通过" },
]);
// 定义表格列配置
const fileColumns = ref([]);
const nameId = ref([]);
const projectId = ref("");
const passNum = ref(0);
// 定义表格数据
const filetable = ref([]);
const restoretable = ref([]);
const Stop = ref(true);
const props = defineProps({
  type: {
    type: String,
    required: true,
  },
  projectInfo: {
    type: String,
    required: true,
  },
  pattern: {
    type: String,
    required: true,
  },
});
const getInfo = () => {
  restoretable.value = [];
  applySupplierInfoByProjectId({ id: id.value }).then((res) => {
    if (res.code == "200") {
      filetable.value = res.data;
      passNum.value = 0;
      projectId.value = res.data[0].projectId;
      let list = [];
      fileColumns.value = [];
      if (res.data.length > 0) {
        for (let z = 0; z < res.data.length; z++) {
          fileColumns.value.push({
            prop: filetable.value[z].supplierId,
            label: filetable.value[z].name,
            id: filetable.value[z].id,
            supplierId: filetable.value[z].supplierId,
          });
          if (res.data[z].valueList) {
            for (let i = 0; i < res.data[z].valueList.length; i++) {
              if (list[i] && res.data[z].valueList[i].textValue) {
                nameId.value.push({
                  id: res.data[z].valueList[i].id,
                  valueId: res.data[z].valueList[i].valueId,
                  textValue: res.data[z].valueList[i].textValue,
                  name: res.data[z].supplierId,
                  supplierId: res.data[z].valueList[i].supplierId,
                });
                list[i][res.data[z].supplierId] =
                  res.data[z].valueList[i].auditStatus;
              } else if (res.data[z].valueList[i].textValue) {
                list.push({
                  key: i + 1,
                  textValue: res.data[z].valueList[i].textValue,
                  applyId: res.data[z].valueList[i].applyId,
                  id: res.data[z].valueList[i].id,
                });
                nameId.value.push({
                  id: res.data[z].valueList[i].id,
                  valueId: res.data[z].valueList[i].valueId,
                  textValue: res.data[z].valueList[i].textValue,
                  name: res.data[z].supplierId,
                  supplierId: res.data[z].valueList[i].supplierId,
                });
                list[i][res.data[z].supplierId] =
                  res.data[z].valueList[i].auditStatus;
              }
            }
          } else {
            ElMessage({
              message: "供应商为空！",
              type: "error",
            });
          }
        }
      } else {
        ElMessage({
          message: "供应商为空！",
          type: "error",
        });
      }
      list.push({
        textValue: "综合认定",
      });
      for (let i = 0; i < res.data.length; i++) {
        if (res.data[i].status == "apply_supplier_pass") {
          passNum.value++;
        }
        list[list.length - 1][res.data[i].supplierId] = res.data[i].status;
      }
      filetable.value = list;
    }
  });
  if (
    props.projectInfo.ratifyExecTime &&
    (props.projectInfo.ratifyExecFile || props.projectInfo.ratifyExecRemark)
  ) {
    Stop.value = false;
    const extractFileName = (path) => {
      const regex = /[^\\/]+$/; // 匹配最后一个斜杠后的所有字符直到字符串结束
      const match = path.match(regex);
      return match ? match[0] : null;
    };
    restoretable.value.push({
      key: 1,
      reviewTime: props.projectInfo.ratifyExecTime,
      projectName: extractFileName(props.projectInfo.ratifyExecFile),
      pushValue: props.projectInfo.ratifyExecFile,
    });
  } else if (
    props.projectInfo.ratifyExecTime &&
    props.projectInfo.ratifyExecRemark
  ) {
    restoretable.value.push({
      key: 1,
      reviewTime: props.projectInfo.ratifyExecTime,
      projectName: props.projectInfo.ratifyExecRemark,
    });
  }
};
const generatePreviewUrl = (path) => {
  // 确保路径正确，适配后端服务地址
  const baseUrl = import.meta.env.VITE_APP_BASE_API;
  return `${baseUrl}${path}`;
};
const previewFile = (file) => {
  const url = generatePreviewUrl(file.pushValue);
  window.open(url, "_blank");
};
const handleSelectChange = (row, column, select, remark) => {
  let Id = "";
  let valueId = "";
  let bool = false;
  for (let i = 0; i < nameId.value.length; i++) {
    if (
      row.textValue == nameId.value[i].textValue &&
      column.supplierId == nameId.value[i].supplierId
    ) {
      Id = nameId.value[i].id;
      valueId = nameId.value[i].valueId;
      bool = true;
    }
  }
  if (!bool) {
    const params = {
      id: column.id,
      status: select,
      remark: remark,
    };
    updateApplySupplier(params).then((res) => {
      if (res.code == "200") {
        getInfo();
      }
    });
    return;
  }
  const params = {
    id: Id,
    projectId: id.value,
    valueId: valueId,
    auditStatus: select,
  };
  updateSupplierPushValue(params).then((res) => {
    if (res.code == "200") {
      getInfo();
    }
  });
};
const nextStep = () => {
  // if(){

  // }
  for (let i = 0; i < filetable.value.length; i++) {
    let list = 0;
    for (let j = 0; j < fileColumns.value.length; j++) {
      if (filetable.value[i][fileColumns.value[j].prop] != null) {
        list++;
      }
    }
    if (list != fileColumns.value.length) {
      ElMessage({
        message: "请完善审核信息",
        type: "error",
      });
      return;
    }
  }
  if (props.type == "taskExamine") {
    const params = { projectId: id.value };
    execTask(params).then((res) => {
      if (res.code) {
        window.location.replace("");
      }
    });
  } else {
    emit("next-step", "taskPrice");
  }
};
const ClickRow = (item) => {
  HiddenArea.value = false;
  setTimeout(() => {
    info.value = { id: id.value, judgeid: item.id };
    HiddenArea.value = true;
  }, 0); // 延迟1000毫秒（即1秒）
};
onMounted(() => {
  getDictList("apply_supplier_status").then((res) => {
    options.value = res;
  });
  getInfo();
});
</script>
<style scoped lang="scss">
@use "./judge";
</style>
